package pl.register.android.activities.tasks.builders;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import pl.register.android.R;
import pl.register.android.task.Task;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-12
 */
public class TaskEditorDialogBuilder extends AlertDialog.Builder {

    private EditText etTitle;

    private EditText etMessage;

    public TaskEditorDialogBuilder(Context context, Task task) {
        super(context);

        onCreateContent(context, task);
    }

    private void onCreateContent(Context ctx, Task task) {
        LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
        View dialogLayout = inflater.inflate(R.layout.dialog_task_editor_layout, null);
        etTitle = (EditText) dialogLayout.findViewById(R.id.task_title_edit_text);
        etMessage = (EditText) dialogLayout.findViewById(R.id.task_message_edit_text);

        setIcon(R.drawable.ic_tasks_holo);
        setView(dialogLayout);
        if (task.isPersisted()) {
            setTitle(R.string.dialog_title_edit_task);
        } else {
            setTitle(R.string.dialog_title_add_task);

        }
        fillDialogFields(task);
    }

    private void fillDialogFields(Task task) {
        etTitle.setText(task.getTitle());
        etMessage.setText(task.getMessage());
    }

    public Task getValuesFromFields(Task task) {
        task.setTitle(etTitle.getText().toString());
        task.setMessage(etMessage.getText().toString());
        return task;
    }

}